package com.baseframe.tool.utils.data;

import java.util.Collection;
import java.util.stream.Collectors;

public class StringUtil {

    //空的

    public static boolean isEmpty(String str){
        if (str==null||str.length()==0){
            return true;
        }
        return false;
    }

    public static boolean isNotEmpty(String str){
        return !isEmpty(str);
    }

    public static boolean isBlank(String str){
        int strLen;
        if (str != null && (strLen = str.length()) != 0) {
            for(int i = 0; i < strLen; ++i) {
                if (!Character.isWhitespace(str.charAt(i))) {
                    return false;
                }
            }

            return true;
        } else {
            return true;
        }
    }

    public static boolean isNotBlank(String str){
        return !isBlank(str);
    }

    public static<T> String join(Collection<T> coll, String delim){

        if (CollectionUtil.isEmpty(coll)){
            return "";
        }

        return coll.stream().map(Object::toString).collect(Collectors.joining(delim));

    }

    public static<T> String join(Collection<T> coll){
        return join(coll,",");
    }

    public static Object format(long lastExecuteTimeNano) {
        if (lastExecuteTimeNano < 1) {
            return "0ms";
        }
        double millis = (double) lastExecuteTimeNano / (1000 * 1000);
        // 不够 1 ms，最小单位为 ms
        if (millis > 1000) {
            return String.format("%.3fs", millis / 1000);
        } else {
            return String.format("%.3fms", millis);
        }
    }
}
